
package com.xebest.util;

import com.xebest.common.enums.erp.order.VenOrderMarkEnum;
import com.xebest.constant.XeConstans;
import com.xebest.web.form.XeVenOrderForm;
import com.xebest.web.model.XeVenOrderInfoDetailEntity;
import com.xebest.web.model.XeVenOrderInfoEntity;
import com.xebest.web.view.ErpUserInfo;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OrderBeanUtils extends BeanUtils {

    /**
     * 转换为订单明细
     */
    public static XeVenOrderInfoEntity toXeVenOrderInfoEntity(XeVenOrderForm xeVenOrderForm) {
        XeVenOrderInfoEntity orderInfoEntity = new XeVenOrderInfoEntity();
        copyProperties(xeVenOrderForm, orderInfoEntity);
        return orderInfoEntity;
    }

    /**
     * 转换为订单明细
     *
     * @author ruancz
     */
    public static XeVenOrderInfoDetailEntity toXeVenOrderInfoDetailEntity(XeVenOrderForm xeVenOrderForm) {
        XeVenOrderInfoDetailEntity orderInfoDetailEntity = new XeVenOrderInfoDetailEntity();
        copyProperties(xeVenOrderForm, orderInfoDetailEntity);
        return orderInfoDetailEntity;
    }

    /**
     * 订单实体装换
     *
     * @author ruancz
     */
    public static List<XeVenOrderInfoEntity> toXeVenOrderInfoEntity(List<XeVenOrderForm> orderForm, ErpUserInfo userInfo, String flag) {
        List<XeVenOrderInfoEntity> list = new ArrayList<XeVenOrderInfoEntity>();
        for (int i = 0; i < orderForm.size(); i++) {
            XeVenOrderInfoEntity infoEntity = new XeVenOrderInfoEntity();
            copyProperties(orderForm.get(i), infoEntity);
            //锁单标识操作
            if (flag.equals(XeConstans._XE_STR_ONE)) {
                if (null == infoEntity.getOrderMark()) {
                    infoEntity.setOrderMark(VenOrderMarkEnum.LOCK.getValue());
                } else {
                    String orderMark = OrderBeanUtils.removeOrderMark(infoEntity.getOrderMark().split(","), VenOrderMarkEnum.LOCK.getValue());
                    if (StringUtils.isNotBlank(orderMark)) {
                        infoEntity.setOrderMark(orderMark + "," + VenOrderMarkEnum.LOCK.getValue());
                    } else {
                        infoEntity.setOrderMark(VenOrderMarkEnum.LOCK.getValue());
                    }
                }
            } else {
                String[] orderMark = infoEntity.getOrderMark().split(",");
                String join = removeOrderMark(orderMark, VenOrderMarkEnum.LOCK.getValue());
                infoEntity.setOrderMark(join);
            }
            infoEntity.setUserCode(userInfo.getUserCode());
            infoEntity.setModfiyUser(userInfo.getLoginName());
            list.add(infoEntity);
        }
        return list;

    }

    /**
     * 删除订单标记
     *
     * @author ruancz
     */
    public static String removeOrderMark(String[] orderMark, String flag) {
        Arrays.sort(orderMark);
        int search = Arrays.binarySearch(orderMark, flag);
        if (search >= 0) {
            String[] remove = (String[]) ArrayUtils.remove(orderMark, search);
            String newOrderMark = StringUtils.join(remove, ",");
            return newOrderMark;
        } else {
            String newOrderMark = StringUtils.join(orderMark, ",");
            return newOrderMark;
        }
    }
}
  
